package JDOM;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import model.Rank;

public class JDOM_XML_Ranks {

	SAXBuilder builder;
	File xmlFile;

	public JDOM_XML_Ranks() {
		builder = new SAXBuilder();
		xmlFile = new File("./XML/ranks.xml");
	}

	public List<Rank> getRanks() {

		List<Rank> ranks = new ArrayList<>();
		try {

			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			List<Element> list = rootNode.getChildren("rank");

			for (int i = 0; i < list.size(); i++) {

				Element node = (Element) list.get(i);

				System.out.println("\nLevel: " + node.getChildText("level"));
				System.out.println("Points: " + node.getChildText("points"));
				System.out.println("Prize : " + node.getChildText("prize"));

				Rank r = new Rank();
				r.setLevel(node.getChildText("level"));
				r.setPoints(Integer.valueOf(node.getChildText("points")));
				r.setPrize(node.getChildText("prize"));
				ranks.add(r);
			}

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
		return ranks;
	}

	public void addRank(Rank newR) {

		try {

			Document document = (Document) builder.build(xmlFile);

			Element staff = new Element("rank");

			staff.addContent(new Element("level").setText(newR.getLevel()));
			staff.addContent(new Element("points").setText(""
					+ newR.getPoints()));
			staff.addContent(new Element("prize").setText(newR.getPrize()));

			document.getRootElement().addContent(staff);

			// new XMLOutputter().output(doc, System.out);
			XMLOutputter xmlOutput = new XMLOutputter();

			// display nice nice
			xmlOutput.setFormat(Format.getPrettyFormat());
			xmlOutput.output(document, new FileWriter("./XML/ranks.xml"));

			System.out.println("File Saved!");

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void modifyRank(Rank updRank) {

		try {

			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			List<Element> list = rootNode.getChildren("rank");

			for (int i = 0; i < list.size(); i++) {

				Element node = (Element) list.get(i);

				if (node.getChildText("level").equals(updRank.getLevel())) {
					node.getChild("points").setText("" + updRank.getPoints());
					node.getChild("prize").setText(updRank.getPrize());
				}

			}

			XMLOutputter xmlOutput = new XMLOutputter();

			// display nice nice
			xmlOutput.setFormat(Format.getPrettyFormat());
			xmlOutput.output(document, new FileWriter("./XML/ranks.xml"));

			System.out.println("File Saved!");

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}

	}

	public Rank findRank(int points) {

		try {

			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			List<Element> list = rootNode.getChildren("rank");

			for (int i = 0; i < list.size() - 1; i++) {

				Element node = (Element) list.get(i);
				Element node1 = (Element) list.get(i + 1);

				if (Integer.valueOf(node.getChildText("points")) < points
						&& Integer.valueOf(node1.getChildText("points")) > points) {

						Rank rank = new Rank();
						rank.setLevel(node.getChildText("level"));
						rank.setPoints(Integer.valueOf(node.getChildText("points")));
						rank.setPrize(node.getChildText("prize"));
						
						return rank;
				}

			}


		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
		
		return null;

	}

}
